﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GBIT.Web;
using System.Linq;
using System.Linq.Expressions;
using WebComponents;
using WebComponents.Data;
using System.Collections.Generic;
using WebComponents.Security;

public partial class ThemNhomSanPham : UserControlBase {
  protected void Page_Load(object sender, EventArgs e) {
    this.Title = "Chuyên mục";
    this.TaskTitle = "Thêm mới";
    if (!Page.IsPostBack) {
      //LoadCategory(CatetoryType.News);
      if (ToInt(Request.Params["id"]) > 0) {
        var cate = CategoryController.GetByCategoryID(ToInt(Request.Params["id"]));
        if (cate == null)
          return;
        cmbParentCate.Items.Clear();
        cmbParentCate.Items.Add(new ListItem("", "0"));
        Global.LoadCategory(cmbLanguage.SelectedValue, ref cmbParentCate, (CatetoryType)cate.CateType);
        cmbParentCate.SelectedIndex = -1;
        if (cmbParentCate.Items.FindByValue(cate.ParentID.ToString()) != null) {
          cmbParentCate.Items.FindByValue(cate.ParentID.ToString()).Selected = true;
        }
        if (ToString(Request.Params["l"]) == "vi" || Request.Params["l"] == null) {
          txtTitle.Text = cate.NameVi;
          txtTrichDan.Value = cate.FullTextVi;
          txtMoTaNgan.Value = cate.DescriptionVi;
          cmbLanguage.SelectedIndex = 0;
        } else {
          txtTitle.Text = cate.NameEn;
          txtTrichDan.Value = cate.FullTextEn;
          txtMoTaNgan.Value = cate.DescriptionEn;
          cmbLanguage.SelectedIndex = 1;
        }
        chkStatus.Checked = cate.IsActive;
        cmbTheLoaiChuyenMuc.SelectedIndex = -1;
        if (cmbTheLoaiChuyenMuc.Items.FindByValue(cate.CateType.ToString()) != null)
          cmbTheLoaiChuyenMuc.Items.FindByValue(cate.CateType.ToString()).Selected = true;
        txtOrderId.Text = cate.OrderID.ToString();
        //ImagePath.ImagePath = cate.ImageUrl;
        //ImagePath.ImageUrl = cate.ImageUrl;
        cmbTheLoaiChuyenMuc.Enabled = false;
        this.TaskTitle = "Cập nhật";
        this.ApplyEnabled = true;
        this.CancelEnabled = true;
      } else
        LoadCategory();
    }
    this.SaveEnabled = true;
  }
  protected override void OnSaveRecord(bool redirect) {
    if (txtTitle.Text.Trim() == "") {
      Security.ShowClientMessageError(Page, "Nhập tên chuyên mục.");
      return;
    }

    //if (txtImage.Text.Trim() == "") {
    //  Security.ShowClientMessage(Page, "Chọn hình ảnh nhóm sản phẩm");
    //  return;
    //}
    //if (txtTrichDan.Value.Trim() == "") {
    //  Security.ShowClientMessage(Page, "Nhập nội dung mô tả nhóm sản phẩm");
    //  return;
    //}
    CategoryInfo cate = null;
    if (ToInt(Request.Params["id"]) > 0)
      cate = CategoryController.GetByCategoryID(ToInt(Request.Params["id"]));
    if (cate == null)
      cate = new CategoryInfo();
    if (ToInt(cmbParentCate.SelectedValue) == cate.CategoryID && cate.CategoryID != 0) {
      Security.ShowClientMessageError(Page, "Chuyên mục cha không hợp lệ. \r\nChuyên mục cha không thể là chính nó");
      return;
    }
    if (cmbLanguage.SelectedValue == "vi-VN") {
      cate.NameVi = txtTitle.Text;
      cate.DescriptionVi = txtMoTaNgan.Value;
      cate.FullTextVi = txtTrichDan.Value;
    } else {
      cate.NameEn = txtTitle.Text;
      cate.DescriptionEn = txtMoTaNgan.Value;
      cate.FullTextEn = txtTrichDan.Value;
    }
    cate.IsActive = chkStatus.Checked;
    //cate.ImageUrl = ImagePath.ImagePath;
    cate.ParentID = ToInt(cmbParentCate.SelectedValue);
    //cate.OrderID = ToInt(txtOrderId.Text);
    cate.CateType = ConvertType.ToInt(cmbTheLoaiChuyenMuc.SelectedValue);
    if (ToInt(Request.Params["id"]) == 0) {
      CategoryController.Insert(cate);
      Security.ShowClientMessage(Page, "Thêm chuyên mục mới thành công", string.Format("?mod={0}", ConvertType.Encode("SanPham/NhomSanPhamList.ascx")));
    } else {
      CategoryController.Update(cate);
      Security.ShowClientMessage(Page, "Cập nhật thông tin chuyên mục thành công", string.Format("?mod={0}", ConvertType.Encode("SanPham/NhomSanPhamList.ascx")));
    }
  }

  protected void LoadCategory() {
    this.cmbParentCate.Items.Clear();
    this.cmbParentCate.Items.Add(new ListItem("", "0"));
    Global.LoadCategory(cmbLanguage.SelectedValue, ref cmbParentCate, (CatetoryType)(ConvertType.ToInt(cmbTheLoaiChuyenMuc.SelectedValue)));
  }
  protected void cmbLanguage_SelectedIndexChanged1(object sender, EventArgs e) {
    LoadCategory();
  }
  protected void cmbTheLoaiChuyenMuc_SelectedIndexChanged(object sender, EventArgs e) {
    //ImagePath.ImagePath = "";
    //txtTitle.Text = "";
    //txtTrichDan.Value = "";
    LoadCategory();
  }
}
